import {
  getActiveList,
  getActiveSetting,
  approveActive
} from '../../../../services/activePlan/index';
const role = wx.getStorageSync('role');
const userId = role?.userId;
import Toast from 'tdesign-miniprogram/toast/index';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    visible: false,
    startTime: '',
    endTime: '',
    timeRange: '',
    pageNo: 1,
    pageSize: 10,
    param: {
      description: '',
      agentUserId: userId,
      auditStatus: '',
      empUserIds: [0],
      title: '',
    },
    datalist: [],
    showConfirm: false,
    confirmBtn: {
      content: '确定',
      variant: 'base'
    },
    setting: 0,
    group: {},
    pullDownRefreshing: false,
    goodsListLoadStatus: 0,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getlist();
    this.loadHomePage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  // 获取列表
  getlist() {
    const {
      startTime,
      endTime,
      timeRange,
      pageNo,
      pageSize,
      param
    } = this.data;
    getActiveList({
      startTime,
      endTime,
      timeRange,
      pageNo,
      pageSize,
      param,
    }).then((res) => {
      if (res.status) {
        this.setData({
          datalist: res.data.entities,
        });
      }
    });
  },
  onReTry() {
    this.loadGoodsList();
  },
  navApprovel(e) {
    const {
      group
    } = e.currentTarget.dataset;
    this.setData({
      showConfirm: true,
      setting: 1,
      group: group,
    });
  },
  del(e) {
    const {
      group
    } = e.currentTarget.dataset;
    this.setData({
      showConfirm: true,
      group: group,
    });
  },
  confirmDialog() {
    const {
      setting
    } = this.data;
    if (setting === 0) {
      //删除
      getActiveSetting({
        kid: this.data.group.kid,
        opType: 1,
      }).then((res) => {
        if (res.status) {
          Toast({
            selector: '#t-toast',
            message: '操作成功',
            icon: '',
            duration: 1000,
          });
          setTimeout(() => {
            this.closeDialog();
            this.getlist();
          });
        }
      });
    } else {
      approveActive({
        // 代理商端需要修改
        auditMemo: '',
        auditStatus: 0,
        kid: this.data.group.kid,
      }).then((res) => {
        if (res.status) {
          Toast({
            selector: '#t-toast',
            message: '操作成功',
            icon: '',
            duration: 1000,
          });
          setTimeout(() => {
            this.closeDialog();
            this.getlist();
          });
        }
      });
    }
  },
  closeDialog() {
    this.setData({
      showConfirm: false,
    });
  },
  seting() {
    this.setData({
      visible: true,
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  handleClick(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/subpages/pages/activePlan/home/index?kid=${id || ''}`,
    });
  },

  async loadGoodsList(fresh = false) {
    const {
      startTime,
      endTime,
      timeRange,
      param
    } = this.data;
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1,
    });
    const {
      pageSize
    } = this.goodListPagination;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const {
        data
      } = await getActiveList({
        pageNo: pageIndex,
        pageSize: pageSize,
        startTime,
        endTime,
        timeRange,
      });
      this.setData({
        datalist: fresh ? data.entities : this.data.entities.concat(data.entities),
        goodsListLoadStatus: pageIndex == 1 && data.entities.length < 10 ? 2 : data.entities.length > 1 ? 0 : 2,
      });

      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3,
      });
    }
  },
  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },
  onReTry() {
    this.loadGoodsList();
  },
});